package com.bridgeintelligent.tag.utils;

import com.bridgeintelligent.tag.constants.TagConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SecurityHelper {
    private static final String GUEST_USERNAME = "guest";

    protected static void currentPermissions(List<String> permissionCodes) {
        Session session = shiroSession();
        List<String> extPermissionCodes = new ArrayList<>();
        for (String permissionCode : permissionCodes) {
            String[] codes = permissionCode.split(",");
            if (StringUtils.isEmpty(permissionCode) || (codes.length < 2)) {
                continue;
            }
            for (String code : codes) {
                extPermissionCodes.add(code.trim());
            }
        }
        permissionCodes.addAll(extPermissionCodes);
        session.setAttribute(TagConstants.SESSION_KEY_OF_PERMISSION, new HashSet<>(permissionCodes));
    }

    public static Set<String> currentPermissions() {
        Session session = shiroSession();
        Object obj = session.getAttribute(TagConstants.SESSION_KEY_OF_PERMISSION);
        if (obj != null) {
            return (Set<String>) obj;
        }
        return null;
    }

    protected static void currentUser(Object user) {
        Session session = shiroSession();
        session.setAttribute(TagConstants.SESSION_KEY_OF_USER, user);
    }

    public static <T> T currentUser() {
        Session session = shiroSession();
        Object obj = session.getAttribute(TagConstants.SESSION_KEY_OF_USER);
        if (obj != null) {
            return (T) obj;
        }
        return null;
    }

    protected static void tagperms(Object tagperms) {
        Session session = shiroSession();
        session.setAttribute(TagConstants.SESSION_KEY_OF_TAGPERMS, tagperms);
    }

    public static <T> T tagperms() {
        Session session = shiroSession();
        Object obj = session.getAttribute(TagConstants.SESSION_KEY_OF_TAGPERMS);
        if (obj != null) {
            return (T) obj;
        }
        return null;
    }

    public static boolean containsPermission(String permissionCode) {
        Session session = shiroSession();
        Object obj = session.getAttribute(TagConstants.SESSION_KEY_OF_PERMISSION);
        Set<String> permissions = (obj == null ? new HashSet<>(1) : (Set<String>) obj);
        return permissions.contains(permissionCode);
    }

    public static Session shiroSession() {
        return SecurityUtils.getSubject().getSession();
    }

    public static void login(UsernamePasswordToken token) {
        SecurityUtils.getSubject().login(token);
    }

    public static void logout() {
        SecurityUtils.getSubject().logout();
    }

    public static boolean isGuestUsername(String username) {
        return GUEST_USERNAME.equals(username);
    }

    public static String guestUsername() {
        return GUEST_USERNAME;
    }

    public static void main(String[] args) {
        System.out.println("abc".split(",").length);
    }
}
